import React, { Component } from 'react';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import { StatusBar, Platform } from 'react-native';
import JPushModule from 'jpush-react-native';
import Home from './pages/home/index';
import Login from './pages/login';
import PhoneLogin from './pages/login/phone';
import ForgetPassword from './pages/login/forgetpassword';
import Register from './pages/register';
import RegisterTags from './pages/register/registerTags';
import SubjectAdd from './pages/subject/add';
import BindPhone from './pages/login/bindPhone';
import Utils from './utils';
import InputBindPhoneCode from './pages/login/inputBindPhoneCode';
import { View } from 'react-native-animatable';
import AppHome from './pages/app';
import FeedBack from './pages/user/feedback';
import UserDetail from './pages/user/detail';
import UserTarget from './pages/user/target';
import UserPoster from './pages/user/poster';
import TaskAdd from './pages/task/add';
import TaskEdit from './pages/task/edit';
import TaskList from './pages/task/list';
import TaskDetail from './pages/task/taskDetail';

/**
 * 状态栏
 */
const initStatusBar = () => {
    StatusBar.setBarStyle('dark-content');
    if (Platform.OS === 'android' && Platform.Version > 22) {
        StatusBar.setBackgroundColor('#ffffff');
    }

}

//初始化状态栏
initStatusBar();

const getOptions = title => ({
    title: title,
    headerStyle: {
        backgroundColor: '#ffffff',
        borderBottomWidth: 0,
        shadowOpacity: 0,
        elevation: 0,
    },
    headerTintColor: 'black',
    headerBackTitle: null,
    gesturesEnabled: true

});




const AppContainer = createAppContainer(createStackNavigator({
    App:{
        screen:AppHome,
        navigationOptions:getOptions("")
    },
    Home: {
        screen: Home,
        navigationOptions:getOptions("")
    },
    Login: {
        screen: Login,
        navigationOptions: getOptions("登录")
    },
    BindPhone:{
        screen: BindPhone,
        navigationOptions: getOptions("")
    },
    InputBindPhoneCode:{
        screen:InputBindPhoneCode,
        navigationOptions:getOptions("")
    },
    PhoneLogin: {
        screen: PhoneLogin,
        navigationOptions: getOptions("填写验证码")
    },
    ForgetPassword: {
        screen: ForgetPassword,
        navigationOptions: getOptions("忘记密码")
    },
    Register: {
        screen: Register,
        navigationOptions: getOptions("注册")
    },
    RegisterTags: {
        screen: RegisterTags,
        navigationOptions:getOptions("")
    },
    SubjectAdd: {
        screen: SubjectAdd,
        navigationOptions:getOptions("雅思答题卡")
    },
    FeedBack: {
        screen: FeedBack,
        navigationOptions: getOptions("意见反馈")
    },
    UserDetail: {
        screen: UserDetail,
        navigationOptions: getOptions("个人资料")
    },
    UserTarget: {
        screen: UserTarget,
        navigationOptions: getOptions("备考目标")
    },
    UserPoster:{
        screen:UserPoster,
        navigationOptions:getOptions("")
    },
    TaskAdd:{
        screen:TaskAdd,
        navigationOptions: getOptions("添加任务")
    },
    TaskEdit:{
        screen:TaskEdit,
        navigationOptions: getOptions("编辑任务")
    },
    TaskList:{
        screen:TaskList,
        navigationOptions: getOptions("")
    },
    TaskDetail:{
        screen:TaskDetail,
        navigationOptions: getOptions("任务详情")
    }
}));

export default class App extends Component {


    state = {
        loginUser:{
            isLogin:false,
            user:{}
        }
    }

    constructor(props) {
        super(props);
        initStatusBar();
       
        Utils.getLoginInfo().then(user=>{

        
            if(user){
                // JPushModule.setAlias(user.id+"",(d)=>{
                //     console.log("setAlias::",d);
                // });
                this.setState({
                    loginUser:{
                        isLogin:true,
                        user:user
                    }
                })
                
            }
            

        });
    }

    componentDidMount() {

        // 新版本必需写回调函数
        // JPushModule.notifyJSDidLoad();
        if (Platform.OS === 'ios') {
            // JPushModule.notifyJSDidLoad((resultCode) => {
            //     console.log("resultCode::",resultCode);
            //     if (resultCode === 0) { }
            // });
            JPushModule.initPush();
            JPushModule.setBadge(0,()=>{
                
            })
        }


       

       // JPushModule.setAlias("")



        // 接收自定义消息
        JPushModule.addReceiveCustomMsgListener((message) => {
            console.log("receive message: ", message);
        });
        JPushModule.addReceiveNotificationListener((msg) => {
            console.log("receive notification: ", msg);
        })
        // 打开通知
        JPushModule.addReceiveOpenNotificationListener((map) => {
            console.log("Opening notification!");
            console.log("map.extra: ", map);
            // 可执行跳转操作，也可跳转原生页面
            // this.props.navigation.navigate("SecondActivity");
        });

    }

    componentWillUnmount() {
        JPushModule.removeReceiveCustomMsgListener();
        JPushModule.removeReceiveNotificationListener();
        JPushModule.removeReceiveOpenNotificationListener();
    }

    render() {
      
        return (
            <AppContainer/>
        )
    }
};